/* 
 * Copyright [2018] [Alex/libo(liboms@hotmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.guava.bio;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import com.hyts.guava.exception.Exceptions;

/** 
 * @title BIO.java  
 * @package com.hyts.guava.bio  
 * @description  Guava框架支持的系统信息<br>
 * 				 Guava 使用术语”流” 来表示可关闭的，并且在底层资源中有位置状态的 I/O 数据流。术语”字节流”指<br>
 * 				  的是 InputStream 或 OutputStream，”字符流”指的是 Reader 或 Writer（虽然他们的接口 Readable <br>
				 和 Appendable 被更多地用于方法参数）。相应的工具方法分别在 ByteStreams 和 CharStreams 中。<br>  
 * @see com.google.common.io.ByteStreams
 * @see com.google.common.io.CharStreams
 * @author libo/Alex
 * @email liboms@hotmail.com/liboware@163.com  
 * @date 2018年5月28日 
 * @version V1.0 
 */
public final class BIO {

	
	private static final Integer INPUT_LIMIT = 100000;

	
	/**  
	 * <b>copyStream</b>
	 * <p>拷贝输入流到输出流-可以直接作为写出文件或者客户端之用</p>
	 * @param src 输入流
	 * @param dest 输出流
	 * @return 字节长度
	 * @exception
	 */ 
	public static long copyStream(InputStream src,OutputStream dest){
		try {
			return ByteStreams.copy(src, dest);
		} catch (IOException e) {
			Exceptions.throwRuntimeException(e);
		}
		return 0L;
	}
	
	/**  
	 * <b>toByteArray</b>
	 * <p>转为字节数组方法</p>
	 * @param src 输入流参数对象
	 * @return 字节数组
	 */ 
	public static byte[] toByteArray(InputStream src){
		try {
			return ByteStreams.toByteArray(src);
		} catch (IOException e) {
			Exceptions.throwRuntimeException(e);
		}
		return new byte[0];
	}
	
	/**
	 * @Title: _read
	 * @Description: 读取数据信息到对应的返回对应的数据信息：
	 * @param in
	 * @param byteArray
	 * @param off
	 * @param length
	 * @return
	 * @return: int
	 * @throws IOException 
	 */
	public int _read(InputStream in,byte[] byteArray,int off,int length) throws IOException{ return ByteStreams.read(in, byteArray, off, length);}
	
	/**
	 * @Title: _readByte
	 * @Description: 读取对应的对象ExtByteProcess<T>泛形类
	 * @param is
	 * @param off
	 * @param length
	 * @return
	 * @throws IOException
	 * @return: T
	 */
	//public static  <T> T _readByte(InputStream is,int off,int length) throws IOException{ return ByteStreams.readBytes(is,new ExtGuavaByteProcess<T>());}
	/**
	 * @Title: _readFull
	 * @Description: 读取输入流InputStream 到 对应的数组中
	 * @param is
	 * @param byteArray
	 * @throws IOException
	 * @return: void
	 */
	public static  void _readFull(InputStream is,byte[] byteArray) throws IOException{  ByteStreams.readFully(is, byteArray);} 
	
	/**
	 * @Title: _readFull
	 * @Description: 读取输入流InputStream 到 对应的数组中(有对应的起始位置和偏移量)
	 * @param is
	 * @param byteArray
	 * @param start
	 * @param length
	 * @throws IOException
	 * @return: void
	 */
	public static  void _readFull(InputStream is,byte[] byteArray,int start,int length) throws IOException{  ByteStreams.readFully(is, byteArray,start,length);} 

	/**
	 * @Title: _skipFull
	 * @Description: 跳过多少长度的数据进行读取数据信息
	 * @param is
	 * @param byteArray
	 * @param start
	 * @param length
	 * @throws IOException
	 * @return: void
	 */
	public  static void _skipFull(InputStream is,byte[] byteArray,long length) throws IOException{  ByteStreams.skipFully(is,length);} 
	
	/**
	 * @Title: createOutputStream
	 * @Description: 创建一个新的OutputStream流类对象
	 * @return
	 * @return: OutputStream
	 */
	public  static OutputStream createOutputStream(){return ByteStreams.nullOutputStream();}
	
	/**
	 * @Title: createInputStream
	 * @Description:  创建一个新的InputStream流类对象
	 * @param is
	 * @param limit
	 * @return
	 * @return: InputStream
	 */
	public  static InputStream createInputStream(int limit){ limit = limit<=0?INPUT_LIMIT:limit; return ByteStreams.limit(new InputStream(){
		@Override
		public int read() throws IOException {
			return 0;
		}},limit);}
	
	/**
	 * @Title: createInputStream
	 * @Description:  创建一个新的InputStream流类对象
	 * @param is
	 * @return
	 * @return: InputStream
	 */
	public static  InputStream createInputStream(){ return ByteStreams.limit(new InputStream(){
		@Override
		public int read() throws IOException {
			return 0;
		}},INPUT_LIMIT);}
	
	
	/**************************************文件操作***********************************************/

	/**
	 * @Title: createParentDirectory
	 * @Description: 创建文件父目录
	 * @param fileName
	 * @return: void
	 * @throws IOException 
	 */
	public static void createParentDirectory(String fileName) throws IOException{ Files.createParentDirs(new File(fileName));}
	
	/**
	 * @Title: getFileExtName
	 * @Description: 返回文件扩展名
	 * @param fileName
	 * @return
	 * @return: String
	 */
	public static String getFileExtName(String fileName){return Files.getFileExtension(fileName);}
	
	/**
	 * @Title: getFileNameExcludeExtName
	 * @Description: 返回文件名 不包含：扩展名
	 * @param fileName
	 * @return
	 * @return: String
	 */
	public  static String getFileNameExcludeExtName(String fileName){return Files.getNameWithoutExtension(fileName);}
	
	/**
	 * @Title: moveFile
	 * @Description: 移动文件
	 * @param sourcePath
	 * @param destPath
	 * @throws IOException
	 * @return: void
	 */
	public static void moveFile(String sourcePath,String destPath) throws IOException{ Files.move(new File(sourcePath), new File(destPath));}
	
	/**
	 * @Title: moveFile
	 * @Description: 移动文件
	 * @param source
	 * @param dest
	 * @throws IOException
	 * @return: void
	 */
	public static void moveFile(File source,File dest) throws IOException{ Files.move(source, dest);}

	
	
}
